#ifndef _WIFI_H
#define _WIFI_H

#include <arpa/inet.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stddef.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/un.h>
#include <sys/wait.h>

#include "list.h"

#define ETH_NAME "wlan0"
#define APINFO_TOTAL_ITEMS 5  // the items count we need for a ap hot spot

struct wpa_ctrl {
    int s;
    struct sockaddr_un local;
    struct sockaddr_un dest;
};

enum { AUTH_NONE_OPEN, AUTH_NONE_WEP, AUTH_NONE_WEP_SHARED, AUTH_IEEE8021X, AUTH_WPA_PSK, AUTH_WPA_EAP, AUTH_WPA2_PSK, AUTH_WPA2_EAP };

typedef enum {
    SCAN_SEC = 0x00,
    CONNECT_SEC,
} SCANAP_TYPE;

typedef struct WifiscanAPInfo {
    char bssid[20];
    int freq;
    int level;
    char flag[50];
    char ssid[32];
    char Keyopen;
} ScanAPInfo;

typedef struct WifiConnectedAPInfo {
    int id;
    char ssid[32];
} ConnectedAPInfo;

typedef struct currSelectAPInfo {
    int id;
    int level;
    char ssid[32];
} CurrAPInfo;

typedef struct HwLocalInfo {
    char bssid[20];
    int freq;
    char ssid[32];
    int id;
    char key_mgmt[20];
    char wap_state[16];
    char ip_address[20];
    char address[20];
} WifiLocalInfo;

typedef struct {
    char *ssid;
    char *passcode;
    char security;
} D_WIFI_CON;

typedef void (*scanResultCb)(List *list);
typedef void (*autoConnCb)(char *ssid);

typedef struct {
    scanResultCb scanCb;
    unsigned int scanSec;
    autoConnCb autoConnCbCallback;
    unsigned int connSec;
} scan_t;

typedef struct {
    autoConnCb autoConnCbCallback;
    char connSec;
} autoConn_t;

char *redbee_strtok(char *s, const char *ct);

int redbee_wifi_init(void);
/**
**function:redbee_wifi_init
**Descirbe: it used to get wifi module name and build a UDP socket with wpa_supplicant
**return: <-1----> error   -1--->time out    0: succeed
*/

void redbee_wifi_close();

int redbee_wifi_connectNewAP(char *ssid, char *passcode, int security, char sec);
int redbee_wifi_disconnect(void);
void redbee_wifi_scan(scanResultCb ScanCb, char scanSec, autoConnCb ConnCb, char connSec);
void redbee_wifi_scan_stop(void);
/**
** function: redbee_wifi_scan
**Describe: the wifi will automatically scan every once in a while and connected the hot spot if there \
                     is a hot spot which is connected before
**param: scanSec [ in ] the scan interval
**param: connSec [ in ] the connect interval
**param: ScanCb [ in ] the scan result report to outside
**param: ConnCb [ in ] the connect result report to outside
*/
int redbee_wifi_connectOldAP(char *ssid, char sec);
int redbee_wifi_removeSelectAP(char *ssid);
char *redbee_wifi_connectSsid();
/**->
** Function:redbee_wifi_connectSsid
** Describe:get the ssid which is in COMPLETED state
** param:ssid [ out ]  the ssid string
** return:  the ssid hot spot which is connected currently
*/

int redbee_wifi_netListWorks(char *ssid);
int redbee_wifi_get_signal(char *ssid, int *signalLevel);
int redbee_wifi_EnableAll();
// void redbee_wifi_autoConn(autoConnCb cb,char sec);
void redbee_wifi_change_scanAP(int type, unsigned int value);

/*Set WiFi scan interval time*/
void redbee_wifi_scan_interval(char sec);

int redbee_wifi_get_psk(char *ssid, char **psk);
int redbee_wifi_enableAllList();

char *redbee_wifi_getMac();
char *redbee_getIpAddr(char *interface);
char *redbee_wifi_getNetMask();
char *redbee_wifi_getGateway();
int redbee_dhcp_get(char *interface);
int redbee_dhcp_release(char *interface);
/*Link Speed*/
int redbee_wifi_link_speed();
#endif
